package ch.abcm.remsmobile.REMSMobile.history;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ch.abcm.remsmobile.REMSMobile.R;

public class MessageHistoryActivity extends Activity {
	
	public static final String INTENT_ACTION = "ch.bfh.abcm.rems.history.UPDATE_HISTORY";
	
	private LinearLayout ll;
	private ScrollView scrollView;
	
	BroadcastReceiver historyreceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			MessageHistoryActivity.this.receiveAction(intent);
		}
	};

	
	private void receiveAction(Intent intent) {
		TextView textView = new TextView(this);
		textView.setText(intent.getStringExtra("message"));
		ll.addView(textView);
		View ruler = new View(this);
		ruler.setBackgroundColor(0x000000);
		ll.addView(ruler, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 10));
		scrollView.post(new Runnable() {
			public void run() {
				scrollView.scrollTo(0, scrollView.getBottom());
			}
		});

	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_history);
		ll = (LinearLayout) findViewById(R.id.ll_historylist);
		scrollView = (ScrollView) findViewById(R.id.sv_history_list);
		IntentFilter filter = new IntentFilter();
		filter.addAction(INTENT_ACTION);
		registerReceiver(historyreceiver, filter);
	}
	
	@Override
	protected void onPause() {
		unregisterReceiver(historyreceiver);
		super.onPause();
	}
	
}
